package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.AssortService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class AssortController {

    @Autowired
    private AssortService assortService;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping("admin/category")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类:{}", categoryDTO);
        assortService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * C端条件查询
     *
     * @param type
     * @return
     */
    @GetMapping("/user/category/list")
    public Result<List<Category>> findByTypeOfC(Integer type) {
        log.info("C端条件查询:{}", type);
        List<Category> categories = assortService.findByType(type);
        return Result.success(categories);
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("admin/category/page")
    public Result findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询:{}", categoryPageQueryDTO);
        PageResult pageResult = assortService.findByPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("admin/category")
    public Result deleteAssort(Integer id) {
        log.info("根据id删除分类,当前id是:{}", id);
        assortService.deleteAssort(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @return
     */
    @GetMapping("admin/category/list")
    public Result<List<Category>> findByType(Integer type) {
        log.info("根据类型查询分类,类型:{}", type);
        List<Category> categorys = assortService.findByType(type);
        return Result.success(categorys);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping("admin/category")
    public Result updateAssort(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类:{}", categoryDTO);
        assortService.updateAssort(categoryDTO);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("admin/category/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Integer id) {
        log.info("修改分类状态:{},id是:{}", status, id);
        assortService.updateStatus(status, id);
        return Result.success();
    }
}
